package com.tqjc.system.core.redis;

import com.alibaba.fastjson2.support.spring.data.redis.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

/**
 * @title RedisConfig.java
 * @description 
		  redis 配置类
 * @time  2019年12月17日 下午4:41:31
 * @author 麻木神
 * @version 1.0
*/
@Configuration
public class RedisConfig {
    public void setSerializer(RedisTemplate template){
        FastJson2JsonRedisSerializer fastJsonRedisSerializer = new FastJson2JsonRedisSerializer(Object.class);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(fastJsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(fastJsonRedisSerializer);
        template.afterPropertiesSet();

//        // 创建 JSON 序列化工具
//        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
//        // 设置 key 的序列化
//        template.setKeySerializer(RedisSerializer.string());
//        template.setHashKeySerializer(RedisSerializer.string());
//        // 设置 value 的序列化
//        template.setValueSerializer(jsonRedisSerializer);
//
//
//        template.afterPropertiesSet();
    }

//    @Configuration
//    public class RedisTemplateConfig {
//        @Bean
//        @Primary
//        public RedisTemplate<String,Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
//            RedisTemplate<String,Object> template = new RedisTemplate<>();
//            template.setConnectionFactory(redisConnectionFactory);
//            FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
//            // key 的序列化方式为 string 序列化方式
//            template.setKeySerializer(new StringRedisSerializer());
//            template.setHashKeySerializer(new StringRedisSerializer());
//            template.setValueSerializer(fastJsonRedisSerializer);
//            template.setHashValueSerializer(fastJsonRedisSerializer);
//            template.setDefaultSerializer(fastJsonRedisSerializer);
//            template.afterPropertiesSet();
//            return template;
//        }
//    }
	
}
